package com.liyuan.yiqixing.service.impl;

import com.liyuan.yiqixing.dao.AuthenticationDao;
import com.liyuan.yiqixing.model.User;
import com.liyuan.yiqixing.service.AuthenticationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class AuthenticationServiceImpl implements AuthenticationService {
    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;

    @Autowired
    AuthenticationDao authenticationDao;

    @Override
    public void insertPreUser(User user) {
        authenticationDao.insertPreUser(user);
    }

    @Override
    public User retrieveUser(String account) {
        return authenticationDao.retrieveUserByAccount(account);
    }

    @Override
    public void insertUser(User user) {
        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
        user.setAvatarUrl("/images/adorable.jpg");//注册时默认头像
        authenticationDao.insertUser(user);
    }

    @Override
    public User retrieveUser(Integer id) {
        User user = authenticationDao.retrieveUserById(id);
        return user;
    }

    @Override
    public void modifyUser(User user) {
        authenticationDao.updateUser(user.getId(),user.getNickname(),user.getDescription());
    }

    @Override
    public User retrievePartOfUser(Integer userId) {
        return authenticationDao.selectPartOfUser(userId);
    }

    @Override
    public void updateUserAvatarUrl(Integer userId, String avatarUrl) {
        authenticationDao.updateUserAvatarUrl(userId,avatarUrl);
    }
}
